package com.imddysc.springbootbatch001.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class JobDemo {
	
	@Autowired
	private JobBuilderFactory jobBuilderFactory;
	
	@Autowired
	private StepBuilderFactory stepBuilderFactory;
	
	@Bean
	public Job jobDemoJob() {
		return jobBuilderFactory.get("jobDemoJob")
				.start(jobDemoJobstep1())
				.next(jobDemoJobstep2())
				.next(jobDemoJobstep3())
				.build();
		
		/*
		.start(step1())
		.on("COMPLETED").to(step2())
		.from(step2()).on("COMPLETED").stopAndRestart(step3())
		.from(step3()).end()
		.build();
		*/
	}

	@Bean
	public Step jobDemoJobstep3() {
		return stepBuilderFactory.get("jobDemoJobstep3")
				.tasklet(new Tasklet() {

					@Override
					public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
						System.out.println("jobDemoJobstep3!!!");
						return RepeatStatus.FINISHED;
					}
					
				}).build();
	}

	@Bean
	public Step jobDemoJobstep2() {
		return stepBuilderFactory.get("jobDemoJobstep2")
				.tasklet(new Tasklet() {

					@Override
					public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
						System.out.println("jobDemoJobstep2!!!");
						return RepeatStatus.FINISHED;
					}
					
				}).build();
	}

	@Bean
	public Step jobDemoJobstep1() {
		return stepBuilderFactory.get("jobDemoJobstep1")
				.tasklet(new Tasklet() {

					@Override
					public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
						System.out.println("jobDemoJobstep1!!!");
						return RepeatStatus.FINISHED;
					}
					
				}).build();
	}
	
}
